package com.itheima.controller;

import com.itheima.pojo.PmsProductCategory;
import com.itheima.service.PmsProductCategoryService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.PmsProductCategoryVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService service;

    //添加商品分类
    @PostMapping("/create")
    public ResponseEntity create(@RequestBody PmsProductCategory category) {
        service.save(category);
        return ResponseEntity.ok(Result.success(null));
    }

    //删除商品分类
    @PostMapping("/delete/{id}")
    public ResponseEntity delete(@PathVariable("id") Long id) {
        service.delete(id);
        return ResponseEntity.ok(Result.success(null));
    }

    //c查询所有分类及其子分类
    @GetMapping("/list/withChildren")
    public ResponseEntity listwithChildren() {
        List<PmsProductCategoryVo> vos = service.getAllCategory();
        return ResponseEntity.ok(Result.success(vos));
    }

    //分页查询商品分类
    @GetMapping("/list/{parentId}")
    public ResponseEntity listParentId(@PathVariable("parentId") Long pid,
                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResultVo vo = service.getPages(pid, pageNum, pageSize);
        return ResponseEntity.ok(Result.success(vo));
    }
    //修改navStatus
    @PostMapping("/update/navStatus")
    public ResponseEntity updateNavStatus(@RequestParam Integer ids,
                                          @RequestParam Integer navStatus) {
        service.updateNavStatus(ids, navStatus);
        return ResponseEntity.ok(Result.success(null));
    }
    //修改update/showStatus
    @PostMapping("/update/showStatus")
    public ResponseEntity updateShowStatus(@RequestParam Integer ids,
                                           @RequestParam Integer showStatus){
        service.updateShowStatus(ids, showStatus);
        return ResponseEntity.ok(Result.success(null));
    }
    //修改分类信息
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@PathVariable("id")Long id
            ,@RequestBody PmsProductCategory category){
        service.updateById(id,category);
        return ResponseEntity.ok(Result.success(null));

    }
    //用于修改的页面数据回显
    @GetMapping("/{id}")
    public ResponseEntity getById(@PathVariable("id")Long id){
        PmsProductCategory byId = service.getById(id);
        return ResponseEntity.ok(Result.success(byId));
    }
}
